package com.easykoo.ioc.property;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;

import com.easykoo.ioc.BeanDefinitionLoader;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.easykoo.ioc.BeanFactory;
import com.easykoo.ioc.Exception.BeanWireException;
import com.easykoo.ioc.Exception.NoSuchBeanException;

public class PropertyBeanDefinitionLoader implements BeanDefinitionLoader {
    private Log logger = LogFactory.getLog(this.getClass());
    private BeanFactory beanFactory;

    public PropertyBeanDefinitionLoader(String filename) throws Exception {
        logger.debug("Start to load " + filename);
        beanFactory = PropertyBeanFactory.getInstance();
        Properties properties = new Properties();
        InputStream inputFile = new FileInputStream(this.getClass().getClassLoader()
                .getResource(filename).getPath());
        properties.load(inputFile);
        Map<String, Object> beanMap = PropertyParser.parse(properties);
        logger.debug("Loaded " + filename);
        beanFactory.push(beanMap);

    }

    public Object getBean(String beanName) throws NoSuchBeanException, BeanWireException {
        return beanFactory.getBean(beanName);
    }

}
